package model;

import java.util.*;

/**
 * A template used to create the hotel floors.   
 * 
 * @author Kenny Chong, Dan End, James Ju
 * CSE 333 : User Interface
 * Project HMSystem
 */
public class Floor 
{
	// Tracker for rooms in the floor.
	private ArrayList<Room> roomList;
	
	// Tracker for elevators in the floor.
	private ArrayList<Elevator> elevatorList;
	
	// Number of rooms in the floor.
	private int numOfRooms;
	
	// Number of elevators in the floor.
	private int numOfElevators;
	
	// Floor name.
	private String floorName;
	
	// File reader, use file information to create room.
	private FileRead reader;
	
	// File directory of room information.
	private static final String dir = "data/room.txt";
			
	/**
	 * Constructor.
	 */
	public Floor(String floorName, int numOfRooms, int numOfElevators)
	{
		this.floorName = floorName;
		this.numOfRooms = numOfRooms;
		this.numOfElevators = numOfElevators;
		
		roomList = new ArrayList<Room>();
		elevatorList = new ArrayList<Elevator>();
		
		// Initialize file reader.
		reader = new FileRead(dir);
	}
	
	public Floor(String floorName)
	{
		this.floorName = floorName;
		
		roomList = new ArrayList<Room>();
		elevatorList = new ArrayList<Elevator>();
		
		// Initialize file reader.
		reader = new FileRead(dir);
		
		// Create rooms.
		this.createRoom();
	}
	
	/**
	 * Create a new room from file data.
	 */
	public void createRoom()
	{
	
		String roomName = "";
		RoomType roomType = null;
		int x = 0;
		int y = 0;
		
		ArrayList<String> list = reader.parse();
		
		for (String s : list)
		{

		    if (s.startsWith("Room")) {
				// Retrieve room name, which should be 
				// something like: "Room Number #".
				roomName = s.substring(0, s.length()-1);
			} else if (s.startsWith("x") || s.startsWith("X")) {
				x = parseIntValue(s);
			} else if (s.startsWith("y") || s.startsWith("Y"))  {
				y = parseIntValue(s);
			} else if (s.startsWith("Type")) {
				roomType = parseRTValue(s);
			} else if (s.equals("-End")) {
				
				// Create a new room.
				Room room = new Room(roomType, this, roomName, x, y);
				
				// Add it to the room list.
				roomList.add(room);
				
				// Increment number of rooms.
				numOfRooms++;
			}
		}
	}
	
	private int parseIntValue(String s)
	{
		int value = 0;
		
				s = s.substring(s.lastIndexOf(": ") + 2);
				
				// Attempt to parse value into integer.
				try 
				{
					value = Integer.parseInt(s);
				} catch (NumberFormatException e) {
					System.out.println(e);
				}
		
		return value;
	}
	
	private RoomType parseRTValue(String s)
	{
		RoomType value = null;
		
				s = s.substring(s.lastIndexOf(": ") + 2);
				
				if (s.equals("SINGLE"))
				{
					value = RoomType.SINGLE;
				} else if (s.equals("DOUBLE")) {
					value = RoomType.DOUBLE;
				} else if (s.equals("SPECIAL")) {
					value = RoomType.SPECIAL;
				}

		return value;
	}
	
	/**
	 * Retrieve room list.
	 */
	public ArrayList<Room> getRMList()
	{
		return roomList;
	}
	
	/**
	 * Retrieve floor name.
	 */
	public String getFloorName()
	{
		return floorName;
	}
	
	/**
	 * Set floor name.
	 */
	public void setFloorName(String floorName)
	{
		this.floorName = floorName;
	}
	
	/**
	 * Search for guests.
	 */
	public Guests searchForGuest(String guestName)
	{
		for (Room r : roomList)
		{
			// Retrieve guestList.
			for (Guests guest : r.getGuestList())
			{
				if (guest.getName().toLowerCase()
						.equals(guestName.toLowerCase())) 
				{
					return guest;
				}
			}
		}
		
		return null;
	}
}
